package com.b2b2c.store.single.logic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import com.b2b2c.store.single.entity.GoodsClass;
import com.b2b2c.store.single.mapper.GoodsClassMapper;
/**
  *    商品分类
 * @author Administrator
 *
 */
public class GoodsClassLogic extends AbstractLogic<GoodsClass, Object> 
{

	public GoodsClassLogic(HttpServletRequest req) {
		super(req);
		// TODO Auto-generated constructor stub
	}

	@Override
	public Map<String, Map<String, String>> getValidateRule() {
		// TODO Auto-generated method stub
		
		Map<String, Map<String, String>> validate = new HashMap<String, Map<String,String>>();
		
		Map<String, String> message = new HashMap<String, String>();
		
		message.put("checkNumber", "id必须是数字");
		
		validate.put("goods_class_id", message);
		
		return validate;
	}
	
	/**
	 * 
	 * @return
	 */
	public List<GoodsClass> getClassList(RedisTemplate<String, List<GoodsClass>> redisTemplate) {
		
		Integer pId = Integer.valueOf(this.getMap().get("goods_class_id"));
		
		ValueOperations<String, List<GoodsClass>> valueOption =  redisTemplate.opsForValue();
		
		String key = pId + "goods_class";
		
		List<GoodsClass> list = valueOption.get(key);
		
		if(list != null) {
			return list;
		}
		
		List<GoodsClass> classList = this.getSqlTemp().getMapper(GoodsClassMapper.class).selectByPid(pId);
		
		this.getSqlTemp().clearCache();
		
		if (classList == null) {
			return classList;
		}
		
		valueOption.set(key, classList, 60, TimeUnit.SECONDS);
		
		return classList;
	}
	

}
